/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.jprvyd.gui.model;

import cz.muni.fi.pb138.jprvyd.PaymentMethod;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Jan
 */
public class PaymentMethod_TableModel extends AbstractTableModel {
    
    List<PaymentMethod> methods;
    private static int COLUMN_COUNT = 2;
    
    public PaymentMethod_TableModel(List<PaymentMethod> methods) {
        this.methods = methods;
        
    }  

    @Override
    public int getRowCount() {
        return methods.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        PaymentMethod method = methods.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return method.getId();
            case 1:
                return method.getName();
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }
    
    public void addMethod(PaymentMethod method) {
        methods.add(method);
        int lastRow = getRowCount() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }

    public void editMethod(PaymentMethod method) {
        int index = methods.indexOf(method);
        methods.set(index, method);
        fireTableRowsUpdated(index, index);
    }

    public void deleteMethod(int row) {
        methods.remove(methods.get(row));
        fireTableRowsDeleted(row, row);
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return Long.class;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }
}
